package mobi.hami.huapin.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.cache.SpyMemcachedManager;
import mobi.hami.huapin.config.MemcacheConfig;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.ProductForUserDTO;
import mobi.hami.huapin.model.Product;
import mobi.hami.huapin.model.base.PageVO;
import mobi.hami.huapin.service.ProductForEndService;
import mobi.hami.huapin.service.ProductService;
import mobi.hami.huapin.service.SubjectService;
import mobi.hami.huapin.service.impl.ProductServiceImpl;
import mobi.hami.huapin.util.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 商品 相关api
 *
 * @author 阿飞 jijunjian@hami.mobi
 * @date 2018/8/16 16:32
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/product", produces = "application/json")
@Api(description = "商品相关接口api")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);


    @Autowired
    private ProductService productService;

    @Autowired
    private MemcacheConfig memcacheConfig;

    @Autowired
    private SpyMemcachedManager memcachedManager;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存商品信息（spu）", notes = " ")
    public Result mytest(@RequestBody Product product) {
        int rs = productService.save(product);

        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存商品信息（spu）", notes = " ")
    public Result update(@RequestBody Product product) {
        int rs = productService.update(product);

        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/list.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取spu", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "subject_id", value = "用户名", example = "abc", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result getList(@RequestParam int subject_id) {
        List<Product> products = productService.getTestList(subject_id);

        return Results.successWithData(products, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getOne.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取spu", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "spu_id", value = "spu编号", example = "abc", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result getOne(@RequestParam Long spu_id) {
        Product product = productService.getOney(spu_id);

        return Results.successWithData(product, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/delete.do", method = RequestMethod.DELETE)
    @ApiOperation(value = "获取spu", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "spu_id", value = "spu编号", example = "abc", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result delteOne(@RequestParam Long spu_id) {
        int rs = productService.delete(spu_id);

        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/jdbctest.do", method = RequestMethod.DELETE)
    @ApiOperation(value = "jdbctest", notes = " ")
    public Result jdbcTest() {
        int rs = productService.jdbcTest();

        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/memcachesettest.do", method = RequestMethod.DELETE)
    @ApiOperation(value = "memcache 设置", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "key", value = "key", example = "abc", paramType = "query", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "value", value = "value", example = "abc", paramType = "query", required = true, dataType = "String")
            }
    )


    public Result memSetTest(@RequestParam String key , @RequestParam String value) {

         memcachedManager.set(key,value,600);
        return Results.successWithData(1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());

    }



    @RequestMapping(value = "/redisgettest.do", method = RequestMethod.DELETE)
    @ApiOperation(value = "redis 获取", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "key", value = "key", example = "abc", paramType = "query", required = true, dataType = "String")
            }
    )

    public Result redisGetTest(@RequestParam String key ) {


        logger.info("redisTemplate key="+key);

        String value = (String) redisTemplate.opsForValue().get(key);

        return Results.successWithData(value, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/redissettest.do", method = RequestMethod.DELETE)
    @ApiOperation(value = "redis 设置", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "key", value = "key", example = "abc", paramType = "query", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "value", value = "value", example = "abc", paramType = "query", required = true, dataType = "String")
            }
    )
    public Result redisSetTest(@RequestParam String key , @RequestParam String value) {


        redisTemplate.opsForValue().set(key,value,600,TimeUnit.SECONDS);

        return Results.successWithData(1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());

    }



    @RequestMapping(value = "/testlist.do", method = RequestMethod.POST)
    @ApiOperation(value = "查询设备列表", notes = "{\n" +
            "  \"subject_id\": \"9\",\n" +
            "  \"name\": \"哈米\"\n" +
            "} ")
    public Result testlist(@RequestBody Map<String, Object> reqMap) {

        //logger.info(JSON.toJSONString(reqMap));


        int pageindex = 1;
        if (reqMap.containsKey("pageindex")) {
            pageindex = Integer.parseInt(reqMap.get("pageindex").toString());
        }
        int pagesize = 10;
        if (reqMap.containsKey("pagesize")) {
            pagesize = Integer.parseInt(reqMap.get("pagesize").toString());
        }
        PageVO vo = productService.getListBoxTest(pageindex, pagesize, "desc", reqMap);

        return Results.successWithData(vo, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());

    }


    @RequestMapping(value = "/queryboxListwithstate.do", method = RequestMethod.POST)
    @ApiOperation(value = "列表-where", notes = "{\n" +
            "  \"code\": \"2222\",\n" +
            "  \"mac\": \"abcd\",\n" +
            "  \"officename\": \"哈米\"\n" +
            "} ")
    public Result queryBoxListWithState(@RequestBody Map<String, Object> reqMap) {


        String sql = " and  1=1";
        if (reqMap.containsKey("subject_id") && reqMap.get("subject_id").toString().length() > 0) {
            sql += " and subject_id='" + reqMap.get("subject_id") + "'";
        }
        if (reqMap.containsKey("name") && reqMap.get("name").toString().length() > 0) {
            sql += " and name like '%" + reqMap.get("name") + "%'";
        }

        int pageindex = 1;
        if (reqMap.containsKey("pageindex")) {
            pageindex = Integer.parseInt(reqMap.get("pageindex").toString());
        }

        int pagesize = 10;
        if (reqMap.containsKey("pagesize")) {
            pagesize = Integer.parseInt(reqMap.get("pagesize").toString());
        }


        PageVO pageVO = productService.getListBox(pageindex, pagesize, "desc", sql);


        return Results.successWithData(pageVO, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());

    }


    @Autowired
    private ProductForEndService productForEndService;
    @Autowired
    private SubjectService subjectService;

    @RequestMapping(value = "/product.do", method = RequestMethod.POST)
    @ApiOperation(value = "微信小程序获取商品列表（有subject_id 根据这个，没有根据周几）", notes = "{\n" +
            "  \"subject_id\": \"主题编号\",\n" +
            "  \"pageindex\": \"1\",\n" +
            "  \"pagesize\": \"20\",\n" +
            "  \"order_name\": \"id\",\n" +
            "  \"order_type\": \"asc\"\n" +
            "}")
    public Result getUserFoods(@RequestBody Map<String, Object> reqMap) {

        PageVO<ProductForUserDTO> pageVO =productForEndService.getListForUserByCountDownPool(reqMap);

        return Results.successWithData(pageVO, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

}
